//
//  ByUISceneHistoryVC.m
//  laserdevice_ios
//
//  Created by pro on 16/12/14.
//  Copyright © 2016年 com.itboye. All rights reserved.
//

#import "ByUISceneHistoryVC.h"

#import "ByUISceneHistoryCell.h"

@interface ByUISceneHistoryVC () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UIControl *showBigImageView;

@property (nonatomic, strong) UICollectionView *collection;

@property (nonatomic, strong) NSArray *items;
@end

@implementation ByUISceneHistoryVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = STRING(@"lishiceliangchangjing");
    
    [self addLeftBackBarButtonItem];

    self.items = [ByFileManager allScenes];
    
    [self createCollectionView];
}

- (void)createCollectionView {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    CGFloat width = (SCREEN_WIDTH) / 2;
    CGFloat height = 100;
    layout.itemSize = CGSizeMake(width, height);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) collectionViewLayout:layout];
    self.collection.backgroundColor = TABLE_COLOR;
    self.collection.delegate = self;
    self.collection.dataSource = self;
    [self.view addSubview:self.collection];
    [self.collection registerClass:[ByUISceneHistoryCell class] forCellWithReuseIdentifier:@"ByUISceneHistoryCell"];
}

#pragma mark - UICollectionViewDatasource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.items.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ByUISceneHistoryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ByUISceneHistoryCell" forIndexPath:indexPath];
    cell.jsonDic = self.items[indexPath.item];
    cell.delectAction = ^ {
      
        [UIAlertView showWithTitle:STRING(@"shanchuchangjing") message:STRING(@"shanchuchangjing-jinggao") cancelButtonTitle:STRING(@"fou") otherButtonTitles:@[STRING(@"shi")] tapBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
            
            if (buttonIndex == 1) {
                NSString *sceneName = self.items[indexPath.item][@"name"];
                [ByFileManager deleteSceneWithName:sceneName
                                           success:^{
                                               NSMutableArray *temp = [[NSMutableArray alloc] initWithArray:self.items];
                                               [temp removeObjectAtIndex:indexPath.row];
                                               self.items = temp.copy;
                                               [self.collection reloadData];
                                           }
                                           failure:^(NSError *error) {
                    
                                               NSLog(@"删除失败:%@", error.domain);
                                           }];
                
                
            }
        }];
    };
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
 
    NSDictionary *jsonDic = self.items[indexPath.row];
    
    NSString *source_type = jsonDic[@"source_type"];
    if ([source_type isEqualToString:@"PDF"]) {

        [self lookBigImage:jsonDic[@"name"]];
    } else {
        [UIAlertView showWithTitle:STRING(@"diaoyongchangjing") message:STRING(@"diaoyongchangjing-jinggao") cancelButtonTitle:STRING(@"woyaoxianbaocun") otherButtonTitles:@[STRING(@"diaoyongchangjing")] tapBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
            
            if (buttonIndex == 1) {
                if (self.backAction) {
                    self.backAction(jsonDic);
                    [self dismissViewControllerAnimated:YES completion:nil];
                }
            }
        }];
    }
}


- (void)lookBigImage:(NSString *)name {
    
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    self.showBigImageView = [[UIControl alloc] initWithFrame:keyWindow.bounds];
    self.showBigImageView.backgroundColor = [UIColor blackColor];
    [self.showBigImageView addTarget:self action:@selector(removeBigImageView) forControlEvents:UIControlEventTouchUpInside];
    
    UIImageView *copyView = [[UIImageView alloc] init];
    UIImage *image = [ByFileManager getImageAtScene:name
                                          imagePath:[NSString stringWithFormat:@".//%@.png", name]];
    copyView.image = image;
    copyView.userInteractionEnabled = NO;
    [self.showBigImageView addSubview:copyView];
    copyView.tag = 100;
    
    CGRect frame = CGRectMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, 0, 0);
    [keyWindow addSubview:self.showBigImageView];
    [keyWindow bringSubviewToFront:self.showBigImageView];
    copyView.frame = frame;
    
    CGFloat x, y, width, height;
    width = keyWindow.width;
    height = width * (image.size.height / image.size.width) ;
    x = 0;
    y = (keyWindow.height - height) / 2;
    CGRect endFrame = CGRectMake(x, y, width, height);
    [UIView animateWithDuration:0.5 animations:^{
        
        copyView.frame = endFrame;
    }];
}

- (void)removeBigImageView {

    UIView *copyView = [self.showBigImageView viewWithTag:100];
    [UIView animateWithDuration:0.5 animations:^{
        
        copyView.frame = CGRectMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, 0, 0);;
    } completion:^(BOOL finished) {
        [self.showBigImageView removeFromSuperview];
    }];
}



@end
